#include <stdio.h>
#include <assert.h>

typedef int SLTDateType;
typedef struct ListNode {
	int val;
	struct ListNode *next;
	
}SLNode;

// 动态申请一个节点 
extern SLNode* BuySListNode(SLTDateType x);

// 单链表打印 
void SListPrint(SLNode* plist);

// 单链表尾插 
void SListPushBack(SLNode** pplist, SLTDateType x);


// 动态申请一个节点 
SLNode* BuySListNode(SLTDateType x)
{
	SLNode* node = (SLNode*)malloc(sizeof(SLNode));
	if (NULL == node){
		assert(0);
		return NULL;
	}
	node->val = x;
	node->next = NULL;
	return node;
}

// 单链表打印 
void SListPrint(SLNode* plist)
{

	SLNode* cur = plist;
	while (cur){                //如果链表不为空
		printf("%d->", cur->val);
		cur = cur->next;
	}

	printf("NULL\n");
}

// 单链表尾插 
void SListPushBack(SLNode** pplist, SLTDateType x)
{
	assert(pplist);         //检验链表是否存在
	if (NULL == *pplist){        //如果链表为空
		*pplist = BuySListNode(x);
		return;
	}
	else{                    //链表不为空
		SLNode* cur = *pplist;
		while (cur->next){
			cur = cur->next;
		}
		cur->next = BuySListNode(x);
	}
}

/*
//循环
struct ListNode* reverseList(struct ListNode* head){
	if (NULL == head){
		return NULL;
	}
	struct ListNode* back = NULL;
	struct ListNode* cur = head;
	struct ListNode* prev = head;
	while (cur != NULL)
	{
		prev = cur->next;
		if (NULL == prev){
			head = cur;
		}
		cur->next = back;
		back = cur;
		cur = prev;
	}
	return head;
}
*/

//递归
struct ListNode* reverseList(struct ListNode* head){
	if (NULL == head || NULL == head->next){
		return head;
	}
	struct ListNode* Newhead= reverseList(head->next);
	head->next->next = head;
	head->next = NULL;
	return Newhead;
}

int main()
{
	SLNode* head = NULL;
	SListPushBack(&head, 1);
	SListPushBack(&head, 2);
	SListPushBack(&head, 3);
	SListPushBack(&head, 4);
	SListPushBack(&head, 5);
	SListPrint(head);

	//反转链表
	SListPrint(reverseList(head));
	return 0;
}